package com.teach.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teach.commons.Id;
import com.teach.dto.NewsSearch;
import com.teach.entity.News;
import com.teach.mapper.NewsMapper;
import com.teach.service.NewsService;

@Service
public class NewsServiceImpl implements NewsService {

	@Autowired
	private NewsMapper mapper;
	
	@Autowired
	private Id id;
	
	@Override
	public void add(News news) {
			news.setNewsId(id.nextId());
			news.setNewsTime(new Date());
			mapper.insert(news);

	}

	@Override
	public void modify(News news) {
			mapper.updateByPrimaryKeySelective(news);
	}

	@Override
	public void delete(Long newsId) {
			mapper.deleteByPrimaryKey(newsId);

	}

	@Override
	public News get(Long newsId) {
			return mapper.selectByPrimaryKey(newsId);
	}

	@Override
	public List<News> list(int startIndex, int len) {
			PageInfo<News> pageInfo = PageHelper.offsetPage(startIndex, len).setCount(false).doSelectPageInfo(()->mapper.select(null));
			return pageInfo.getList();
	}

	@Override
	public PageInfo<News> page(int current, int rowMax) {
			return PageHelper.startPage(current, rowMax).doSelectPageInfo(()->mapper.select(null));
	}

	@Override
	public PageInfo<News> pageSearch(NewsSearch search) {
			return PageHelper.startPage(search.getCurrent(), search.getRowMax()).doSelectPageInfo(()->mapper.select(search));
	}
	
	/**
	 * 按作者查询文章
	 * @param newsWorker
	 * @return
	 */
	@Override
	public List<News> selectByNewsWorker(String newsWorker) {
		return mapper.selectByNewsWorker(newsWorker);
	}

	@Override
	public News getByNewsTitle(String newsTitle) {
		// TODO Auto-generated method stub
		return mapper.selectByTitle(newsTitle);
	}

	@Override
	public List<News> getlist() {
		// TODO Auto-generated method stub
		return mapper.select1();
	}

	@Override
	public PageInfo<News> pageSearchAdmin(NewsSearch search) {
		// TODO Auto-generated method stub
		return PageHelper.startPage(search.getCurrent(), search.getRowMax()).doSelectPageInfo(()->mapper.selectAdmin(search));
	}
	
	@Override
	public Long getByNewsTitleCount(String newsTitle) {
		return mapper.countByTitle(newsTitle);
	}

	@Override
	public List<News> indexlist() {
			// TODO Auto-generated method stub
			return mapper.select2();
		
	}


}
